import requests
import time
import re
import json


def get_data(keyword):
    cookies = {
        'FANYI_WORD_SWITCH': '1',
        'BIDUPSID': 'C94E3A93E4911656B7BAA64FA37EBFCF',
        'PSTM': '1715585246',
        'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574': '1718624925',
        'smallFlowVersion': 'new',
        'MCITY': '-217%3A',
        'BAIDUID': 'B1C8F2836A23E40FEF5239E02C5E47CC:FG=1',
        'H_WISE_SIDS_BFESS': '60600_60623_60665_60677',
        'BDUSS': '9MTnE5T0lJeEd1cy15cHM1STMtM1RlWDZ1RGphSGxNdzlnZ2NGV0t5OVhJfkptRVFBQUFBJCQAAAAAAAAAAAEAAAAHq2GgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFeWymZXlspmYm',
        'BDUSS_BFESS': '9MTnE5T0lJeEd1cy15cHM1STMtM1RlWDZ1RGphSGxNdzlnZ2NGV0t5OVhJfkptRVFBQUFBJCQAAAAAAAAAAAEAAAAHq2GgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFeWymZXlspmYm',
        'BDORZ': 'FFFB88E999055A3F8A630C64834BD6D0',
        'BA_HECTOR': '8g2524a0048ha02g8524042g3s19us1jd0cj51v',
        'ZFY': 'WMaN3X:A:BfQqNFuA0wdvZ2GweGPw58j7UTQ4kh3EGHc4:C',
        'BAIDUID_BFESS': 'B1C8F2836A23E40FEF5239E02C5E47CC:FG=1',
        'H_PS_PSSID': '60600_60665_60677_60572',
        'H_WISE_SIDS': '60600_60665_60677_60572',
        'BDRCVFR[V3EOV_cRy1C]': 'mk3SLVN4HKm',
        'delPer': '0',
        'PSINO': '2',
        'ab_sr': '1.0.1_ZWYzZTBkZDBmNzU3MDQ4ZTdkNTNhNzllOGZiMjM1YTc3ZmVhZGE2NmE2OWNmMDgzZWM0MjA4YmVlZjA2OThjNmNhOTQ5YzVjOTQ5MjAyZWIwZTlkN2I0YjdiYzRjMTI1YThlMTM0ODg1N2QzYWVmOWE2ZmVkN2QwMmY3ZjgzZTMxZTI0OGJjMDY5MzVmMzdiNzkzNWY4MDcyNWUxNmU3Yw==',
        'RT': '"z=1&dm=baidu.com&si=de946ead-b907-4d6f-8a60-eaed2c55373d&ss=m0gc5f5w&sl=1&tt=21l&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=2ts"',
    }

    headers = {
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Acs-Token': '1724925607740_1724999488827_SZwT2a2GCBY+oh44cYp9mYe/6XAhoXHl9miTkbW6apPI+ZSsmEKvWYaaURyxeldYnYu9jJAFOuLGA9E1IT6jgSfwdubTzchL0pjyo23QyFqyEYnPUW7UoXWFX8IxwzvR+VP42Hu6E6o31HBQAfklTDWU+f3MiMxE2xG8WI/D4bi02XW3ciXYYWKFRPL8/saFXnZRykxots+rXvaRgpyp9ox4XRYc+MWLa1qD0XJIIM11VxGh5wTYn/8JZVanA+zWPArm3CzOuKWz/2MQlBiKaeboTP/ORxC4kUUVxcvNCFG+tCS+gAZUAwJVC8sOeryUnBsyMANPY7vQVQexWJrFzZgCqom9ajhOE5sMn/iHBURtFeofu+py2XlK2EYyBaosUN0yIY0JFfgpW9QhTuCdK4O0pGEKc1Y2mkQh6uekBKaGCCciojJfyRrT8xW3SxcACKs3GYEwnfea6uc59med1CKFqBETu5DU3lEdlg92leCNlAZNlpChg+0WspVGxuG0',
        'Connection': 'keep-alive',
        'Content-Type': 'application/json',
        # 'Cookie': 'FANYI_WORD_SWITCH=1; BIDUPSID=C94E3A93E4911656B7BAA64FA37EBFCF; PSTM=1715585246; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1718624925; smallFlowVersion=new; MCITY=-217%3A; BAIDUID=B1C8F2836A23E40FEF5239E02C5E47CC:FG=1; H_WISE_SIDS_BFESS=60600_60623_60665_60677; BDUSS=9MTnE5T0lJeEd1cy15cHM1STMtM1RlWDZ1RGphSGxNdzlnZ2NGV0t5OVhJfkptRVFBQUFBJCQAAAAAAAAAAAEAAAAHq2GgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFeWymZXlspmYm; BDUSS_BFESS=9MTnE5T0lJeEd1cy15cHM1STMtM1RlWDZ1RGphSGxNdzlnZ2NGV0t5OVhJfkptRVFBQUFBJCQAAAAAAAAAAAEAAAAHq2GgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFeWymZXlspmYm; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BA_HECTOR=8g2524a0048ha02g8524042g3s19us1jd0cj51v; ZFY=WMaN3X:A:BfQqNFuA0wdvZ2GweGPw58j7UTQ4kh3EGHc4:C; BAIDUID_BFESS=B1C8F2836A23E40FEF5239E02C5E47CC:FG=1; H_PS_PSSID=60600_60665_60677_60572; H_WISE_SIDS=60600_60665_60677_60572; BDRCVFR[V3EOV_cRy1C]=mk3SLVN4HKm; delPer=0; PSINO=2; ab_sr=1.0.1_ZWYzZTBkZDBmNzU3MDQ4ZTdkNTNhNzllOGZiMjM1YTc3ZmVhZGE2NmE2OWNmMDgzZWM0MjA4YmVlZjA2OThjNmNhOTQ5YzVjOTQ5MjAyZWIwZTlkN2I0YjdiYzRjMTI1YThlMTM0ODg1N2QzYWVmOWE2ZmVkN2QwMmY3ZjgzZTMxZTI0OGJjMDY5MzVmMzdiNzkzNWY4MDcyNWUxNmU3Yw==; RT="z=1&dm=baidu.com&si=de946ead-b907-4d6f-8a60-eaed2c55373d&ss=m0gc5f5w&sl=1&tt=21l&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=2ts"',
        'Origin': 'https://fanyi.baidu.com',
        'Referer': 'https://fanyi.baidu.com/mtpe-individual/multimodal?query=%E4%BB%8A%E5%A4%A9%E6%98%AF%E4%B8%AA%E5%A5%BD%E6%97%A5%E5%AD%90&lang=zh2en&ext_channel=Aldtype',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
        'accept': 'text/event-stream',
        'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Microsoft Edge";v="128"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }

    json_data = {
        'query': keyword,
        'from': 'zh',
        'to': 'en',
        'reference': '',
        'corpusIds': [],
        'qcSettings': [
            '1',
            '2',
            '3',
            '4',
            '5',
            '6',
            '7',
            '8',
            '9',
            '10',
            '11',
        ],
        'needPhonetic': False,
        'domain': 'common',
        'milliTimestamp': int(time.time() * 1000),
    }

    response = requests.post('https://fanyi.baidu.com/ait/text/translate', cookies=cookies, headers=headers, json=json_data)
    return response


if __name__ == '__main__':
    keyword = input("请输入要翻译的语句：\n")
    response = get_data(keyword)
    s = ''.join(response.text.split('\n'))
    res = re.findall('''data: {"errno":0,"errmsg":"Success","data":{"event":"Translating","message":"翻译中","list":(.*?)}}''', s)[0][1:-1]
    trans = json.loads(res)['dst']
    print(trans)
